﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmTimKiemDL : DevExpress.XtraEditors.XtraForm
    {
        string _dbf; string _filldbf; string _keysearchf; int _columnumberf; string _columtitlef; string _columFillf;
        public string _chonKey;
        public string _noidungKey;

        public frmTimKiemDL()
        {
            InitializeComponent();
        }
        public frmTimKiemDL(string _db, string _filldb, string _keysearch, int _columnumber, string _columtitle, string _columFill)
        {
            InitializeComponent();
            _dbf = _db;
            _filldbf = _filldb;
            _keysearchf = _keysearch;
            _columnumberf = _columnumber;
            _columtitlef = _columtitle;
            _columFillf = _columFill;            
            kiemtrabien();
        }

        void kiemtrabien()
        {
            string[] _title =_columtitlef.Split(',');
            string[] _fill = _columFillf.Split(',');
            if ((_keysearchf.Length>0)&&(_columnumberf==_title.Length))
            {    
                switch (_columnumberf)
                {
                    case 1:
                        Colum1.Caption = _title[0].ToString();
                        Colum1.FieldName = _fill[0].ToString();
                        Colum2.Visible = false;
                        Colum3.Visible = false;
                        Colum4.Visible = false;
                        Colum5.Visible = false;
                        break;
                    case 2:
                        Colum1.Caption = _title[0].ToString();
                        Colum2.Caption = _title[1].ToString();
                        Colum1.FieldName = _fill[0].ToString();
                        Colum2.FieldName = _fill[1].ToString();
                        Colum3.Visible = false;
                        Colum4.Visible = false;
                        Colum5.Visible = false;
                        break;
                    case 3:
                        Colum1.Caption = _title[0].ToString();
                        Colum2.Caption = _title[1].ToString();
                        Colum3.Caption = _title[2].ToString();
                        Colum1.FieldName = _fill[0].ToString();
                        Colum2.FieldName = _fill[1].ToString();
                        Colum3.FieldName = _fill[2].ToString();
                        Colum4.Visible = false;
                        Colum5.Visible = false;
                        break;
                    case 4:
                        Colum1.Caption = _title[0].ToString();
                        Colum2.Caption = _title[1].ToString();
                        Colum3.Caption = _title[2].ToString();
                        Colum4.Caption = _title[3].ToString();
                        Colum1.FieldName = _fill[0].ToString();
                        Colum2.FieldName = _fill[1].ToString();
                        Colum3.FieldName = _fill[2].ToString();
                        Colum4.FieldName = _fill[3].ToString();
                        Colum5.Visible = false;
                        break;
                    case 5:
                        Colum1.Caption = _title[0].ToString();
                        Colum2.Caption = _title[1].ToString();
                        Colum3.Caption = _title[2].ToString();
                        Colum4.Caption = _title[3].ToString();
                        Colum5.Caption = _title[4].ToString();
                        Colum1.FieldName = _fill[0].ToString();
                        Colum2.FieldName = _fill[1].ToString();
                        Colum3.FieldName = _fill[2].ToString();
                        Colum4.FieldName = _fill[3].ToString();
                        Colum5.FieldName = _fill[4].ToString();
                        break;
                }              

            }
        }

        private void loadds()
        {
            if (_keysearchf.Length > 0)
            {
                string strSQL = "select * from " + _dbf + " where " + _filldbf + " like '%" + _keysearchf + "%'";
                DataTable dt = Class.DataServices.ExecQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    gridSearch.DataSource = dt;
                }
                else
                {
                    MessageBox.Show(" Không tìm thấy dữ liệu tìm kiếm !");                    
                    this.Close();
                }
            }
            else if (_keysearchf == " ")
            {
                string strSQL = "select * from " + _dbf ;
                DataTable dt = Class.DataServices.ExecQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    gridSearch.DataSource = dt;
                }
                else
                {
                    MessageBox.Show(" Không tìm thấy dữ liệu tìm kiếm !");
                    this.Close();
                }
            }

        }
        private void loaddl2cot() // su dung cho muc tim kiem thiet bi ban giao
        {
            string strSQL = "";
            if (_keysearchf == " ")
            {
                strSQL = "select *  from MT_MayViTinh where MT_MayViTinh.MaMay not in(select CV_BanGiaoThietBi.MaMay from CV_BanGiaoThietBi)";
            }else
            {
                strSQL = "select *  from MT_MayViTinh mt,CV_BanGiaoThietBi bg where mt.MaMay=bg.MaMay and bg.Giu='1' and bg.NguoiNhan='"+_keysearchf+"'";
            }
            DataTable dt= Class.DataServices.ExecQuery(strSQL);
            gridSearch.DataSource = dt;

        }

        private void frmTimKiemDL_Load(object sender, EventArgs e)
        {
            if (_dbf.IndexOf(",") < 0)
            {
                loadds();
            }
            else
            {
                loaddl2cot();
            }
        }

        private void gridDataSearch_DoubleClick(object sender, EventArgs e)
        {
            if (gridDataSearch.DataRowCount > 0)
            {
                int SelectedRow = gridDataSearch.FocusedRowHandle;
                if (SelectedRow >= 0)
                {
                    DataRow dr = gridDataSearch.GetDataRow(SelectedRow);
                    _chonKey = dr[0].ToString();                 
                    if (_columnumberf > 1)
                    {                                               
                        _noidungKey = dr[1].ToString();                       
                    }
                    this.Close();
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
        

    }
}